
#include "kb.h"                 // keybaord event type 
#include "touch_algorithm.h"    // touch event type 
#include "custom_event.h"
#include "gui.h"                // GUI header

#include "views.h"              // include all views 
#include "log.h"                // LOG file 
#define LOG     LOG_INFO0

#include "xapp_profile.h"
#include "xapp_clock.h"
/* 1. include function file */
#include "xapp_battery.h"
#include "xapp_system.h"

enum fsmBatteryCharging
{
    fsmBatteryChargingIdle = 0,
    fsmBatteryChargingUpdate,
    fsmBatteryChargingBackground,
}; 

// declare function 
GUI_VIEW_FUNC_DECLARE_ALL;

static uint8_t percent = 0;
static uint8_t battery_level_value[4]; 

const static ui_widgets_type_t wglBatteryCharging/*1. modify name*/[] = 
{
    // 背景
    [0] = uiCanvas(0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ),        
    // 图标
    [1] = uiPictureBox( IMG_Notification_Charging_Icon_038_065_01, 0, SIZE_SCALE(108), SIZE_SCALE(100), SIZE_SCALE(25), SIZE_SCALE(43) ),
    // circle bar
    [2] = uiCirclebar( SIZE_SCALE(120), SIZE_SCALE(120), 0, 0, COLOR_GRAY2, SIZE_SCALE(80), SIZE_SCALE(8)),
    // circle bar2
    [3] = uiCirclebar( SIZE_SCALE(120), SIZE_SCALE(120), 0, 0, 0xc954, SIZE_SCALE(82), SIZE_SCALE(12) ),
    // percent
    [4] = uiNumberWithBuffer( 0, SIZE_SCALE(110), SIZE_SCALE(240), SIZE_SCALE(26), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%d%%", battery_level_value, 4 ),
};

const static ui_refresh_part_t rectPartBatteryCharging[] = 
{
    [0] = xRECTANGLE(SIZE_SCALE(30), SIZE_SCALE(30), SIZE_SCALE(180), SIZE_SCALE(180) )
};


GUI_VIEW_DEF_NR( viewNotif_BatteryCharging ) = 
{
    // name 
    .name = "Notif+BatteryCharing",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,            
        
    .pWidgetlist =  (ui_widgets_type_t*)wglBatteryCharging /* 3. fill with widgets list name */,
    .widget_cnt = WIDGET_COUNT( wglBatteryCharging ), /* 3. fill with widgets list name */

    .flag = GUI_VIEW_FLAG_FUNC
};


/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0

*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 1000;
    const profile_system_t* p_profile_system = xapp_profile_get_system();
    menu->lifetime = p_profile_system->time_of_light_on * 1000;

    // views_set_fsm( fsmBatteryChargingUpdate );    
    views_animate_set_count( 2 ); 
}

static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{

}

/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{    
    // const xapp_battery_desc_t* p_bat_desc = xapp_battery_get_desc();
    // if( p_bat_desc->charging )
    // {
    //     return true; 
    // }

    // views_set_fsm( fsmBatteryChargingBackground );
    return GUI_DESTROY_RET_DEFAULT; 
}


/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;
    const xapp_battery_desc_t* p_bat_desc = xapp_battery_get_desc();

    if( IS_REQ_SRC_CREATE( menu->source ) )
    {
        // 环形条
        PROP_ASSIGN_CIRCLEBAR( prop, 0, 360 );
        PROP_UPDATE_AT_NO( 2 );

        // 充电进度条
        PROP_ASSIGN_CIRCLEBAR( prop, 0, 0 );
        PROP_UPDATE_AT_NO( 3 );
        
        widgets[1].visible = 1;
        widgets[4].visible = 0;      

        gui_part_t part = GUI_PART_CONFIG( rectPartBatteryCharging, 30 );
        gui_part_create( &part );
        percent = 0;
    }
    else if( menu->source == GUI_VIEW_REQ_SRC_UPDATE_AUTO )
    {
        if( views_animate_get_idx_by_step() & 0x01 )
        {
            widgets[1].visible = 1;
            widgets[4].visible = 0;
        }
        else 
        {
            widgets[1].visible = 0;
            widgets[4].visible = 1;

            PROP_ASSIGN_NUMBER( prop, p_bat_desc->level );
            PROP_UPDATE_AT_NO( 4 );
        }
    }
    else if( menu->source == GUI_VIEW_REQ_SRC_UPDATE_PART )
    {
        percent += 3;
        if( percent > p_bat_desc->level )
        {
            gui_part_stop();
            percent = p_bat_desc->level;
        }

        PROP_ASSIGN_CIRCLEBAR( prop, 0, percent * 360 / 100 );
        PROP_UPDATE_AT_NO( 3 );
        
        gui_update_part();
    }
    
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    // nothings to do
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    kb_evt_t* kb = (kb_evt_t*)pvParameters;

    const xapp_battery_desc_t* p_bat_desc = xapp_battery_get_desc();
    const user_system_desc_t* p_sys_user = xapp_system_current();

    switch( kb->type )
    {
        case KB_EVT_CLICK:
        {
            const user_system_desc_t* p_user_sys = xapp_system_current();
            if( p_user_sys->mode == SYSTEM_MODE_NORMAL) // 仅正常模式下才允许转入表盘界面
            {
                gui_view_create( views_load_current_watchface() );
            }
        }
        break;
    }
}

/* 界面的自定义输入事件处理

*/
static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    custom_evt_t* custom = (custom_evt_t*)pvParameters;
    if( custom->type == CUSTOM_EVT_TYPE_CHARGE_REMOVED )
    {
        gui_lifetime_end(); //关闭显示
    }
}



